package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Animatable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.CartoonHomeBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.StartActivityUtils;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.TimeUtils234;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longsiyang on 16/4/11.
 */
public class DfCartoonHomeItemAdapter extends BaseAdapter
{
    private List<CartoonHomeBean> listData;
    private Context mContext;
    String lastUpdateTime="1";

    public DfCartoonHomeItemAdapter(Context mContext, List<CartoonHomeBean> listData)
    {
        this.mContext = mContext;
        if (listData != null)
        {
            this.listData = listData;
        } else
        {
            this.listData = new ArrayList<>();
        }
    }

    @Override
    public int getCount()
    {
        return listData.size();
    }

    @Override
    public Object getItem(int position)
    {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_cartoonhome_viewholder, null);
            viewHolder.backGroundView = (RelativeLayout) convertView.findViewById(R.id.note_backgroud_layout);
            viewHolder.iv_chapter = (ImageView) convertView.findViewById(R.id.iv_chapter);
            viewHolder.mContentBackgroudView = (LinearLayout) convertView.findViewById(R.id.home_item_subject_image);
            viewHolder.mNoteTitle = (TextView) convertView.findViewById(R.id.personalnote_item_title_textview);
            viewHolder.mUserName = (TextView) convertView.findViewById(R.id.personalnote_item_username_textview);
            viewHolder.mContentImgeView = (SimpleDraweeView) convertView.findViewById(R.id.personal_note_image);
            viewHolder.mContentImgNum = (TextView) convertView.findViewById(R.id.content_img_num);
            viewHolder.mContentImgNumLayout = (LinearLayout) convertView.findViewById(R.id.content_img_num_layout);
            viewHolder.mReadCount = (TextView) convertView.findViewById(R.id.home_item_subject_readtimes);
            viewHolder.mDeleteBtn = (TextView) convertView.findViewById(R.id.delete_item_btn);
            viewHolder.mGifSignIcon = (ImageView) convertView.findViewById(R.id.image_gif_sign_icon);
            viewHolder.rankImageView = (ImageView) convertView.findViewById(R.id.home_rank_position);
            viewHolder.novel_content_textview = (TextView) convertView.findViewById(R.id.novel_content_textview);
            viewHolder.bottom_layout = convertView.findViewById(R.id.home_item_subject_bottom_layout);
            viewHolder.scanCountLayout = (LinearLayout) convertView.findViewById(R.id.scan_count_layout);
            viewHolder.chapterName = (TextView) convertView.findViewById(R.id.tv_chaptername_carttonhome);
            viewHolder.tv_updateTime=(TextView) convertView.findViewById(R.id.tv_cartoonhome_date);
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        changeModel(viewHolder);
        setDefaultStyle(viewHolder);
        setDisplayValue(viewHolder, listData.get(position), position);
        setClickEvent(viewHolder, listData.get(position));
        return convertView;
    }

    private void setClickEvent(ViewHolder viewHolder, final CartoonHomeBean tempCartoonBean)
    {
        viewHolder.mContentBackgroudView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                TMAnalysis.event(mContext, "home2-icon-cartoon");
                if (tempCartoonBean != null) {
                    CartoonServices.enterCartoonScanNewActivity(mContext, tempCartoonBean.getId(), -1,true);
                }
            }
        });
        viewHolder.mNoteTitle.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                TMAnalysis.event(mContext, "home2_icon_cartoondetail");
                Intent intent = new Intent();
                intent.putExtra(Constant.DATA, tempCartoonBean);
                new StartActivityUtils(mContext, intent).toCartoonDesActivity(tempCartoonBean.getId());
            }
        });
        viewHolder.mUserName.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                UserDetailActivity.Companion.startActivity(mContext,tempCartoonBean.getUser().getUserId());
            }
        });
        viewHolder.chapterName.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                TMAnalysis.event(mContext, "home2-icon-cartoon");
                if (tempCartoonBean != null) {
                    CartoonServices.enterCartoonScanNewActivity(mContext, tempCartoonBean.getId(), -1, true);
                }
            }
        });
    }

    private void setDisplayValue(ViewHolder viewHolder, final CartoonHomeBean tempCartoonBean,final int pos)
    {
        if ((tempCartoonBean.getUser() != null) && (tempCartoonBean.getUser().getName() != null))
        {
            viewHolder.mUserName.setText(tempCartoonBean.getUser().getName());
        }
        if ((tempCartoonBean != null) && (tempCartoonBean.getChapters() != null) && (tempCartoonBean.getChapters().size() > 0))
        {
            String tempTitle = tempCartoonBean.getChapters().get(0).getTitle();
            if ((tempTitle != null) && (tempTitle.length() > 0))
            {
                viewHolder.chapterName.setText(tempTitle);
            } else
            {
                String tempTitle2 = tempCartoonBean.getChapters().get(0).getTitle2();
                if ((tempTitle2 != null) && (tempTitle2.length() > 0))
                {
                    viewHolder.chapterName.setText(tempTitle2);
                }
            }

            //更新日期
            viewHolder.tv_updateTime.setVisibility(View.GONE);
            try{
                if((pos==0)||(!TimeUtils234.compareTime(lastUpdateTime,tempCartoonBean.getChapters().get(0).getCreated_at()))){
                    viewHolder.tv_updateTime.setVisibility(View.VISIBLE);
                    viewHolder.tv_updateTime.setText(TimeUtils234.getChineseRuleTime(tempCartoonBean.getChapters().get(0).getCreated_at()));
                    lastUpdateTime=tempCartoonBean.getChapters().get(0).getCreated_at();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        if (tempCartoonBean.getTitle() != null)
        {
            viewHolder.mNoteTitle.setText(tempCartoonBean.getTitle());
            if (tempCartoonBean.getTitle().equals(""))
            {
                viewHolder.mNoteTitle.setVisibility(View.GONE);
            } else
            {
                viewHolder.mNoteTitle.setVisibility(View.VISIBLE);
            }
        } else
        {
            viewHolder.mNoteTitle.setVisibility(View.GONE);
        }
        changeDataShow(viewHolder.mReadCount, tempCartoonBean.getRead_times());

        viewHolder.mContentImgeView.setVisibility(View.VISIBLE);
        viewHolder.mContentImgeView.setBackgroundResource(R.drawable.biankuang_grey_backgroud);
        float imageAspetctRatio = (5.0f / 3.0f);
        String bigImgUrl = "";

        if ((tempCartoonBean != null) && (tempCartoonBean.getChapters() != null) && (tempCartoonBean.getChapters().get(0) != null))
        {
            CartoonHomeBean.ChapterItem chapterItem = tempCartoonBean.getChapters().get(0);
            if ((chapterItem.getCover_5_3() != null) && (chapterItem.getCover_5_3().getImgUrl() != null) && (chapterItem.getCover_5_3().getImgUrl().length() > 0))
            {
                bigImgUrl = chapterItem.getCover_5_3().getImgUrl();
            } else
            {
                bigImgUrl = tempCartoonBean.getImage();
            }
        } else
        {
            bigImgUrl = tempCartoonBean.getImage();
        }

        ImageManager.loadImage(viewHolder.mContentImgeView, bigImgUrl, imageAspetctRatio, new BaseControllerListener<ImageInfo>()
        {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable)
            {
                super.onFinalImageSet(id, imageInfo, animatable);
            }
        });
        if (((tempCartoonBean.getTagImage() != null) && (tempCartoonBean.getTagImage().getImgUrl() != null) && (tempCartoonBean.getTagImage().getImgUrl().length() > 0)))
        {
            viewHolder.iv_chapter.setVisibility(View.VISIBLE);
            ImageManager.displayImage(viewHolder.iv_chapter, tempCartoonBean.getTagImage().getImgUrl());
        } else
        {
            viewHolder.iv_chapter.setVisibility(View.GONE);
        }

        // 如果是gif则显示标志
        if (DiaobaoUtil.isImageUrlGif(tempCartoonBean.getImage()))
        {
            viewHolder.mGifSignIcon.setVisibility(View.VISIBLE);
        } else
        {
            viewHolder.mGifSignIcon.setVisibility(View.GONE);
        }
    }

    private void changeDataShow(TextView tv, String value)
    {
        if ((value != null) && (value.length() > 0))
        {
            try
            {
                int tempValue = Integer.parseInt(value);
                if (tempValue >= 10000)
                {
                    tv.setText((tempValue / 10000)+"."+((tempValue/1000)%10)+ "W");
                } else
                {
                    tv.setText(tempValue + "");
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public void setNewData(List<CartoonHomeBean> listData)
    {
        if (listData != null)
        {
            this.listData = listData;
        } else
        {
            this.listData = new ArrayList<CartoonHomeBean>();
        }
        this.notifyDataSetChanged();
    }

    class ViewHolder
    {
        public RelativeLayout backGroundView;
        public LinearLayout mContentBackgroudView;
        //内容标题
        public TextView mNoteTitle, mUserName;
        //图片及数目
        public SimpleDraweeView mContentImgeView;
        public TextView mContentImgNum,tv_updateTime;
        public LinearLayout mContentImgNumLayout;
        //浏览、
        public TextView mReadCount, mDeleteBtn, chapterName;
        // gif标志
        public ImageView mGifSignIcon, iv_chapter;

        private ImageView novel_end_iv, novel_continue_iv;
        private TextView novel_content_textview;
        private View bottom_layout;
        LinearLayout scanCountLayout;
        // 排名标志
        public ImageView rankImageView;
    }

    private void setDefaultStyle(ViewHolder viewHolder)
    {
        viewHolder.backGroundView.setPadding(ScreenUtil2.dip2px(6), ScreenUtil2.dip2px(5), ScreenUtil2.dip2px(6), 0);
        viewHolder.mNoteTitle.setTextSize(18);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewHolder.mContentImgNumLayout.getLayoutParams();
        layoutParams.setMargins(0, ScreenUtil2.dip2px(10), ScreenUtil2.dip2px(8), 0);
        viewHolder.mContentImgNumLayout.setLayoutParams(layoutParams);
    }

    public void changeModel( ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            changeDarkModel(viewHolder);
        }else {
            changeDayModel(viewHolder);
        }
    }

    private void changeDayModel(  ViewHolder viewHolder){
         Resources resources = WeMediaApplication.getInstance().getResources();
        viewHolder.backGroundView .setBackgroundColor(resources.getColor(R.color.all_activity_background));
        viewHolder.iv_chapter.setAlpha(1.0f);
        viewHolder.mContentBackgroudView .setBackground(resources.getDrawable(R.drawable.bg_home_frame));
        viewHolder.mNoteTitle.setTextColor(resources.getColor(R.color.text_color_12));
        viewHolder.mUserName.setTextColor(resources.getColor(R.color.text_color_35));
        viewHolder.mContentImgeView.setAlpha(1.0f);
        viewHolder.mReadCount.setTextColor(resources.getColor(R.color.text_color_14));
        viewHolder.mGifSignIcon.setAlpha(1.0f);
        viewHolder.rankImageView.setAlpha(1.0f);
        viewHolder.chapterName .setTextColor(resources.getColor(R.color.text_color_14));
        viewHolder.tv_updateTime.setTextColor(resources.getColor(R.color.text_color_34));
     }
    private void changeDarkModel(  ViewHolder viewHolder){
        Resources resources = WeMediaApplication.getInstance().getResources();
        viewHolder.iv_chapter.setAlpha(Constant.imagViewAlpha);
        viewHolder.mContentImgeView.setAlpha(Constant.imagViewAlpha);
        viewHolder.mGifSignIcon.setAlpha(Constant.imagViewAlpha);
        viewHolder.rankImageView.setAlpha(Constant.imagViewAlpha);
        viewHolder.backGroundView .setBackgroundColor(resources.getColor(R.color.bg_dark));
        viewHolder.mContentBackgroudView .setBackgroundColor(resources.getColor(R.color.fg_dark));
        viewHolder.mNoteTitle.setTextColor(resources.getColor(R.color.title_text_dark));
        viewHolder.mUserName.setTextColor(resources.getColor(R.color.text_dark));
        viewHolder.mReadCount.setTextColor(resources.getColor(R.color.text_dark));
        viewHolder.chapterName .setTextColor(resources.getColor(R.color.text_dark));
        viewHolder.tv_updateTime.setTextColor(resources.getColor(R.color.text_dark));

    }
}
